package com.bigData;

import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by chuxw on 2017/12/17.
 */
public class DataProducer {

    private static final Logger logger = LoggerFactory.getLogger(DataProducer.class);

    public static void main(String[] args){
        Properties baseConfiguration = new Properties();
        Properties producerConfiguration = new Properties();
        try {
            baseConfiguration.load(DataProducer.class.getResourceAsStream("/base.properties"));
            if(args != null && args.length > 0 && StringUtils.isNoneBlank(args[0])){
                producerConfiguration.load(new FileInputStream(args[0]));
            }else{
                producerConfiguration.load(DataProducer.class.getResourceAsStream("/producer.properties"));
            }
        }catch (IOException e){
            logger.error("=================加载配置异常=================");
        }


        //发送消息
        Producer producer = new KafkaProducer<String,String>(producerConfiguration);
        for (int i = 1; i <= 10; i++) {
            String value = "value_" + i;
            logger.info("发送的消息: {}", value);
            ProducerRecord<String, String> msg = new ProducerRecord<String, String>(baseConfiguration.getProperty("topic"), value);
            producer.send(msg);
        }
        producer.close();
    }


}
